<?php

namespace app\admin\model;

class Coupon extends Base
{
    // 当前模型名称 不带前缀
    protected $name = 'coupon';

    // 数据表主键 复合主键使用数组定义 不设置则自动获取
    protected $pk = 'id';


    /**
     * 得到优惠券详细信息
     */
    public static function formatCoupon($item){
        if ($item) {
            $item['name'] = show_coupon_type($item['coupon_type']);
            if ($item['coupon_type'] == 2 || $item['coupon_type'] == 5) {
                // 品类券
                //$categoryName = CouponProductCategory::alias('a')->where('a.coupon_id', $item['id'])->join('__PRODUCT_CATEGORY__ b', 'a.category_id = b.id')->column('b.name');
                $categoryIds = CouponProductCategory::where('coupon_id', $item['id'])->value('category_id');
                $categoryName = Productcategory::whereIn('id', $categoryIds)->column('name');
                $categoryName = implode('、', $categoryName);
                //$item['name'] = $categoryName;
                if ($item['coupon_type'] == 2) {
                    $item['name'] = $categoryName . '抵现券';
                } else {
                    $item['name'] = $categoryName . '折扣券';
                }
            } elseif ($item['coupon_type'] == 3 || $item['coupon_type'] == 6) {
                // 单品券
                $productName = CouponProduct::alias('a')->where('a.coupon_id', $item['id'])->join('__PRODUCT__ b', 'a.product_id = b.id')->value('b.name');
                //$item['name'] = $productName;
                if ($item['coupon_type'] == 3) {
                    $item['name'] = $productName . '抵现券';
                } else {
                    $item['name'] = $productName . '折扣券';
                }
            }
        }

        return $item;
    }
}
